<?php
/**
 * QFSHOP 
 * ============================================================================
 * 青峰网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act']=='list'){
	$sql = "SELECT * FROM ".ly()."user_rank order by rank_id";
	/*分页开始*/
	$num='10';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
    $row=$db->getAll($sql);
	$smarty->assign('user',$row);
	$smarty->display('rank_list.html');
}elseif($_REQUEST['act']=='edit'){
	@$rank_id=$_REQUEST['rank_id'];
	if($rank_id>0){
		$sql="select * from ".ly()."user_rank where rank_id='{$rank_id}'";
		$rank_edit=$db->getRow($sql);	
		$smarty->assign('rank_edit',$rank_edit);
	}
	$smarty->display('rank_edit.html');		
}elseif($_REQUEST['act']=='insert'){
	$rank_id = isset($_REQUEST['rank_id'])?$_REQUEST['rank_id']:0;//会员级别ID
	$rank_name = isset($_REQUEST['rank_name'])?$_REQUEST['rank_name']:0;//会员名称
	$min = isset($_REQUEST['min'])?$_REQUEST['min']:0;//最小积分
	$max = isset($_REQUEST['max'])?$_REQUEST['max']:0;//最大积分
	$discount = isset($_REQUEST['discount'])?$_REQUEST['discount']:0;//最大积分
	$special_rank = isset($_REQUEST['special_rank'])?$_REQUEST['special_rank']:0;//是否为特殊会员组
	$show_price = isset($_REQUEST['show_price'])?$_REQUEST['show_price']:0;//是否显示价格
	$rank_num = isset($_REQUEST['rank_num'])?$_REQUEST['rank_num']:0;//会员等级
	if($rank_id>0){
		$sql="update ".ly()."user_rank set rank_name='{$rank_name}',min_points='{$min}',max_points='{$max}',discount='{$discount}',special_rank='{$special_rank}',show_price='{$show_price}',rank_num='{$rank_num}' where rank_id='{$rank_id}'";	
	}else{
		$sql="insert into ".ly()."user_rank(rank_name,min_points,max_points,discount,special_rank,show_price,rank_num) values('{$rank_name}','{$min}','{$max}','{$discount}','{$special_rank}','{$show_price}','{$rank_num}')";
	}
	$res=$db->query($sql);
	if($res){
		 $href[]=array('text'=>"返回会员级别列表",'href'=>'user_rank.php?act=list');
         sys_msg("操作成功！",$href);
	}else{
		$href[]=array('text'=>"重新编辑",'href'=>'history.back()');
        sys_msg("操作失败！",$href,false);
	}
}elseif($_REQUEST['act']=='show_price'){
	$rank_id=isset($_POST['rank_id'])?$_POST['rank_id']:0;
	$show_price=isset($_POST['zt'])?$_POST['zt']:0;
	if($show_price==1){$zt='0';}else{$zt='1';}
	$sql="update ".ly()."user_rank set show_price='{$zt}' where rank_id='{$rank_id}'";
	$res=$db->query($sql);
	if($res){
		echo "1";	
	}else{
		echo "2";
	}
}elseif($_REQUEST['act']=='special_rank'){
	$rank_id=isset($_POST['rank_id'])?$_POST['rank_id']:0;
	$special_rank=isset($_POST['zt'])?$_POST['zt']:0;
	if($special_rank==1){$zt='0';}else{$zt='1';}
	$sql="update ".ly()."user_rank set special_rank='{$zt}' where rank_id='{$rank_id}'";
	$res=$db->query($sql);
	if($res){
		echo "1";	
	}else{
		echo "2";
	}
}elseif($_REQUEST['act']=='del'){
	$rank_id=$_POST['rank_id'];
	//$rank_id=isset($_REQUEST['rank_id'])?$_REQUEST['rank_id']:0;
	$sql="select user_id from ".ly()."users where user_rank='{$rank_id}'";
	$row=$db->getAll($sql);
	if($row){
		echo '0';
	}else{
		$sql="delete from ".ly()."user_rank where rank_id='{$rank_id}'";
        $db->query($sql);
        if(@mysql_affected_rows()){
        	echo "1";
        }else{
            echo "2";
        }
	}
}
?>